
local json = require('json')
local lfs = require('lfs')

local Util = {};

local emptyFn = function()
end



-------------------  JSON  -----------------------
local JSON = {}
JSON.stringify = json.encode
JSON.parse = json.decode

Util.JSON = JSON
--------------------------------------------------

------------------- Network -------------------
local Network = {}

Network.request = function(opts)
	local url, type = opts.url, opts.type or 'GET'
	local dataType = opts.dataType or "text"
	local param = opts.param or {}
	local success = opts.success or emptyFn
	local error = opts.error or emptyFn

	local function networkListener(e)
		if(e.isError) then
			print( "Network error111!" )
			error(e)
		else
			local rs = e.response
			if('json' == dataType) then
				success(JSON.parse(rs))
			else
				success(rs)
			end
		end
	end

	network.request( url, type, networkListener, param )
end

Network.requestGet = function(opts)
	local opts = {
		url = opts.url,
		type = 'GET',
		dataType = opts.dataType or 'json',
		success = opts.success,
		error = opts.error or false
	};
	Network.request(opts)
end

Util.Network = Network
-----------------------------------------------



return Util